package com.example.cinemasystemv2.repository;

import com.example.cinemasystemv2.entity.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface MovieRepository extends JpaRepository<Movie, Long> {
    
    Optional<Movie> findByTitle(String title);
    
    List<Movie> findByDirector(String director);
    
    @Query("SELECT m FROM Movie m WHERE m.title LIKE %:keyword% OR m.director LIKE %:keyword% OR m.actors LIKE %:keyword%")
    List<Movie> searchMovies(@Param("keyword") String keyword);
    
    @Query("SELECT m FROM Movie m WHERE " +
           "(:title IS NULL OR :title = '' OR m.title LIKE %:title%) AND " +
           "(:director IS NULL OR :director = '' OR m.director LIKE %:director%) AND " +
           "(:actors IS NULL OR :actors = '' OR m.actors LIKE %:actors%)")
    List<Movie> searchMoviesByMultipleFields(@Param("title") String title, 
                                           @Param("director") String director, 
                                           @Param("actors") String actors);
    
    @Query("SELECT m FROM Movie m WHERE m.rating >= :minRating")
    List<Movie> findByRatingGreaterThanEqual(@Param("minRating") double minRating);
    
    boolean existsByTitle(String title);
    
    @Query("SELECT COUNT(m) FROM Movie m")
    long countAllMovies();
}